home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / DYNLINK.C < prev    next >
Text File  |  1989-12-30  |  2KB  |  54 lines

  1. #include "stdio.h" /* this is needed only to define the NULL */
  2. #define RECORDS 6
  3.  
  4. main()
  5. {
  6. struct animal {
  7.    char name[25];       /* The animals name                         */
  8.    char breed[25];      /* The type of animal                       */
  9.    int age;             /* The animals age                          */
  10.    struct animal *next; /* a pointer to another record of this type */
  11. } *point, *start, *prior; /* this defines 3 pointers, no variables  */
  12. int index;
  13.  
  14.                        /* the first record is always a special case */
  15.    
  16.    start = (struct animal *)malloc(sizeof(struct animal));
  17.    strcpy(start->name,"General");
  18.    strcpy(start->breed,"Mixed Breed");
  19.    start->age = 4;
  20.    start->next = NULL;
  21.    prior = start;
  22.        /* a loop can be used to fill in the rest once it is started */
  23.  
  24.    for (index = 0;index < RECORDS;index++) {
  25.       point = (struct animal *)malloc(sizeof(struct animal));
  26.       strcpy(point->name,"Frank");
  27.       strcpy(point->breed,"Laborador Retriever");
  28.       point->age = 3;
  29.       prior->next = point;  /* point last "next" to this record */
  30.       point->next = NULL;   /* point this "next" to NULL        */
  31.       prior = point;        /* this is now the prior record     */
  32.    }
  33.  
  34.        /* now print out the data described above */
  35.  
  36.    point = start;
  37.    do {
  38.       prior = point->next;
  39.       printf("%s is a %s, and is %d years old.\n", point->name,
  40.               point->breed, point->age);
  41.       point = point->next;
  42.    } while (prior != NULL);
  43.  
  44.        /* good programming practice dictates that we free up the */
  45.        /* dynamically allocated space before we quit.            */
  46.  
  47.    point = start;            /* first block of group      */
  48.    do {
  49.       prior = point->next;   /* next block of data        */
  50.       free(point);           /* free present block        */
  51.       point = prior;         /* point to next             */
  52.    } while (prior != NULL);  /* quit when next is NULL    */
  53. }
  54.